layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','laydate','table','laytpl','element','util','baseConfig'],function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        baseConfig = layui.baseConfig,
        table = layui.table;
    var util = layui.util;
    var tableIns = table.render({
        elem: '#list',
        url : '/imgTextExtractRule/list/',
        text: { none: "Aucun résultat"},
        cellMinWidth : 95,
        height : "full-125",
        id : "listTable",
        cols : [[
            {field: 'type', title: "规则对应数据项", align:"center", templet : function( d){
                switch ( d.type) {
                    case 1:
                        return "日期";
                        break;
                    case 2:
                        return "金额";
                        break;
                    case 3:
                        return "供应商";
                        break;
                    case 4:
                        return "发票号";
                        break;
                }
            }},
            {field: 'ruleName', title: "规则名称", align:"center"},
            {field: 'rule', title: "匹配规则(正则表达式)", align:"center"},
            {field: 'rowNumber', title: "数据所在行", align:"center"},
            {field: 'colNumber', title: "数据所在列", align:"center"},
            {field: 'createTime', title: '添加时间', align:'center', templet : function( d){
                return util.toDateString( d.createTime);
            }},
            {title: 'État', templet:'#listBar',fixed: "right", align:"center"}
        ]],
        response : {
            statusCode: 200 //规定成功的状态码，默认：0
        },
        parseData : function( res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.length, //解析数据长度
                "data": res.data //解析数据列表
            };
        }
    });

    //添加/修改/详细查看（readonly=true）
    function addOrUpdateOrDetail( data, readonly){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [600 + 'px', 400 + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "imgTextExtractRule", //设定一个id，防止重复弹出
            content : "formData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                if( data){
                    body.find( "input[name=id]").val( data.id).attr( "readonly", readonly);//标识是修改还是新增
                    body.find( "select[name=type]").val( data.type).attr( "readonly", readonly);
                    body.find( "input[name=ruleName]").val( data.ruleName).attr( "readonly", readonly);
                    body.find( "input[name=rule]").val( data.rule).attr( "readonly", readonly);
                    body.find( "input[name=rowNumber]").val( data.rowNumber).attr( "readonly", readonly);
                    body.find( "input[name=colNumber]").val( data.colNumber).attr( "readonly", readonly);
                   if( readonly){
                        body.find( "#submitDiv").hide();
                    }
                    form.render();
                }
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    }

    //删除
    function del( ids){
        layer.confirm('确定删除选中的数据项？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/imgTextExtractRule/del",{
                "ids" : ids
            },function( res){
                if( res.code == 200){
                    tableIns.reload();
                    layer.close( index);
                    top.layer.msg( res.msg);
                }else{
                    top.layer.close( index);
                    top.layer.msg( res.msg);
                }
            });
        });
    }

    //添加
    $(".add_btn").click(function(){
        var index = layui.layer.open({
            title : "添加",
            type : 2,
            area: [700 + 'px', 300 + 'px'],
            shade: 0.4,
            btnAlign: 'r',
            id: "formData_imgTextExtractRule", //设定一个id，防止重复弹出
            content : "formData.html",
            success : function(){
                var body = layui.layer.getChildFrame( 'body', index);
                setTimeout( function(){
                    layui.layer.tips('点击此处返回数据列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500);
            }
        });
    });

    //列表操作
    table.on('tool(list)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            addOrUpdateOrDetail( data, false);
        }else if(layEvent === 'look'){ //预览
            addOrUpdateOrDetail( data, true);
        }else if(layEvent === 'del'){ //删除
            del( data.id);
        }
    });
});
